/*
 * HandleRequest.cpp
 *
 *  Created on: Nov 19, 2011
 *      Author: hao
 */

#include "HandleRequest.h"
#include "HttpsqsMessage.hpp"
#include "HttpsqsMessageHandler.hpp"

#include "dtnhttpcp.h"
#include "dtnhttpcpd.h"
#include "dtnhttprecv.h"
#include "dtnhttpsend.h"
#include "util.h"


#include<stdio.h>   
#include<string.h>   
#include<stdlib.h>   
#include<unistd.h>   
#include<pthread.h>

#include <iostream>
int dtncpd_size=2, dtnrecv_size=2;
char** dtncpd_args;
char** dtnrecv_args;

void processTextRequest(string text_msg)
{
	executeCMD(text_msg);
}

void processXMLRequest(string xmlmsg){
	HttpsqsMessageHandler* handler = new HttpsqsMessageHandler();
	handler->getHttpsqsMessage(xmlmsg);
	HttpsqsMessage* hmsg = handler->getHttpsqsMessage();
	hmsg->setType("Command");
	if(hmsg->getType()=="Command"){
		cout << "execute cmd : " << hmsg->getMessageBody() << endl;
		executeCMD(hmsg->getMessageBody());
	}
}

void* httpdtncpd(void *)
{
	cout << "dtncpd detach thread: " << dtncpd_args[0] << endl;
	excute_dtn_cpd(dtncpd_size, dtncpd_args);
	delete[] dtncpd_args[0];
	delete[] dtncpd_args;
	pthread_exit(NULL);
}

void* httpdtnrecv(void *)
{
	cout << "dtnrecv detach thread: " << dtnrecv_args[0] << endl;
	excute_dtn_recv(dtnrecv_size, dtnrecv_args);
	delete[] dtnrecv_args[0];
	delete[] dtnrecv_args;
	pthread_exit(NULL);
}
  

int executeCMD(string cmd)
{
	cout << "received cmd" << cmd << endl;
	int errocode = 0;
	vector<string> strs = splitEx(cmd, " ");
	int size = strs.size();
	char* argument[size];
	for (int i = 0; i < size; i++) {
		int len = strlen(strs[i].c_str())+1;
		argument[i] = new char[len];
		cout << "not corr" << endl;
		strcpy(argument[i], strs[i].c_str());
		argument[i][len-1] = '\0';
		printf("%s\n",argument[i]);
	}
	cout << "Command:" << argument[1] << endl;
	//excute_dtn_cp(size, argument);
	if(!strcmp(argument[0],"dtncp")){
		excute_dtn_cp(size, argument);
		
	}else if(!strcmp(argument[0],"dtncpd")){
		//char **a;dtncpd_args
		dtncpd_size = size;
		dtncpd_args = new char* [dtncpd_size];
		for (int i = 0; i < dtncpd_size; i++) {
			dtncpd_args[i] = new char[strlen(strs[i].c_str())];
			strcpy(dtncpd_args[i], strs[i].c_str());
			//cout << argument[i] << endl;
		}

		pthread_t pid1;   
    	pthread_attr_t attr;     
       	pthread_attr_init(&attr);          
       	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);   
       	pthread_create(&pid1, &attr, httpdtncpd, NULL);
		
		//excute_dtn_cpd(size, argument);
	}else if(!strcmp(argument[0],"dtnrecv")){
		dtnrecv_size = size;
		dtnrecv_args = new char* [dtnrecv_size];
		for (int i = 0; i < dtnrecv_size; i++) {
			dtnrecv_args[i] = new char[strlen(strs[i].c_str())];
			strcpy(dtnrecv_args[i], strs[i].c_str());
			//cout << argument[i] << endl;
		}

		pthread_t pid;   
    	pthread_attr_t attr;     
       	pthread_attr_init(&attr);          
       	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);   
       	pthread_create(&pid, &attr, httpdtnrecv, NULL);
		//excute_dtn_recv(size, argument);
	}else if(!strcmp(argument[0],"dtnsend")){
		excute_dtn_send(size, argument);
	}

	for (int i = 0; i < size; i++){
		 delete[] argument[i];
	        cout << "free memory" << endl;
	}

	return errocode;
}

